/* 1790. 仅执行一次字符串交换能否使两个字符串相等 */
/**
 * @param {string} s1
 * @param {string} s2
 * @return {boolean}
 */
var areAlmostEqual = function (s1, s2) {
	if (s1.length !== s2.length) return false
	if (s1 === s2) return true
	const temp = [...s2]
	let str1 = [],
		str2 = []
	for (let i = 0; i < s2.length; i++) {
		if (s2[i] !== s1[i]) {
			if (!str1.length) str1 = [i, s2[i]]
			else if (!str2.length) {
				str2 = [i, s2[i]]
				break
			}
		}
	}
	temp[str1[0]] = str2[1]
	temp[str2[0]] = str1[1]

	const _s2 = temp.join('')
	return _s2 === s1
}
// 计数统计
var areAlmostEqual = function () {
	const n = s1.length
	const diff = []
	for (let i = 0; i < n; ++i) {
		if (s1[i] !== s2[i]) {
			if (diff.length >= 2) return false
			diff.push(i)
		}
	}
	if (!diff.length) return true
	if (diff.length !== 2) return false
	return s1[diff[0]] === s2[diff[1]] && s1[diff[1]] === s2[diff[0]]
}
const result = areAlmostEqual((s1 = 'kelb'), 'kelb')
console.log('areAlmostEqual :>>', result)
